package com.xwaiy.xwaiyrpc.loadbalancer;

import com.xwaiy.xwaiyrpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 随机负载均衡器
 * @Author Xwaiy
 * @Date 2025/9/2 17:04
 **/
public class RandomLoadBalancer implements LoadBalancer{
    private Random random = new Random();


    @Override
    public ServiceMetaInfo select(Map<String, Object> params, List<ServiceMetaInfo> list) {
        int size = list.size();

        if(list.isEmpty()){
            return null;
        }

        if (size==1){
            return list.get(0);
        }

        return list.get(random.nextInt(size));
    }
}
